#coding=utf-8
import datetime
import time
import json

#服务名
serviceName = "AMS_TO_CBOSS_ASSET_DORECVFEEBYBANK"
fileName = './场景验证数据/故障1：失败率高20210425_0940-0945/service_fail_20210423162650271.json'


listFile = None
listTraceIds = set()
listTraces = []

try:
    f = open(fileName, 'r', encoding='utf_8')
    listFile = f.readlines()
finally:
    if f:
        f.close()

for jsonStr in listFile:
    jsonStr = jsonStr.replace('false','False')
    jsonStr = jsonStr.replace('true', 'True')
    obj = eval(jsonStr)

    timeStamp = int(obj['startTime'])
    listTraceIds.add(obj['traceId'])

for jsonStr in listFile:
    jsonStr = jsonStr.replace('false', 'False')
    jsonStr = jsonStr.replace('true', 'True')
    obj = eval(jsonStr)

    timeStamp = int(obj['startTime'])


    if obj['traceId'] in listTraceIds:
        listTraces.append(obj)

ret = ''

for obj in listTraces:
    ret += '['
    ret +='\'' + obj['appName'] + '\''
    ret += ','
    ret +='\'' + str(obj['callType']) + '\''
    ret += ','
    ret += str(obj['startTime'])
    ret += ','
    ret += str(obj['elapsedTime'])
    ret += ','
    if 'success' in obj:
        ret += str(obj['success'])
    else:
        ret += '\'\''
    ret += ','
    ret += '\'' + str(obj['traceId']) + '\''
    ret += ','
    ret += '\'' +  obj['serviceName'] + '\''
    ret += ','
    ret += '\'' + obj['id'] + '\''
    ret += ','
    if 'pid' in obj:
        ret += '\'' + obj['pid'] + '\''
    else:
        ret += '\'\''
    ret += ','
    if 'dsName' in obj:
        ret += '\'' + str(obj['dsName']) + '\''
    else:
        ret += '\'\''
    ret += ','
    if 'stack' in obj:
        ret +='\'' + obj['stack'] + '\''
    else:
        ret += '\'\''
    ret += ','
    if 'host' in obj:
        ret += '\'' + obj['host'] + '\''
    else:
        ret += '\'\''
    ret += '];'

with open(fileName + '.param.txt', 'w') as f:
    f.write(ret)
